import vue from "@vitejs/plugin-vue";
import vueJsx from "@vitejs/plugin-vue-jsx";
import { defineConfig } from "vite";
import dts from "vite-plugin-dts";
import wasm from "vite-plugin-wasm";
import topLevelAwait from "vite-plugin-top-level-await";

export default defineConfig({
	base: './', // 使用相对路径，适用于所有部署环境
	server: {
		host: '0.0.0.0', // 允许外部访问
		port: 5173, // 可以自定义端口
		proxy: {
			'/api/image': {
				target: 'http://music.bfmzdx.cn',
				changeOrigin: true,
				rewrite: (path) => path.replace(/^\/api\/image/, ''),
				configure: (proxy, options) => {
					proxy.on('error', (err, req, res) => {
						console.log('proxy error', err);
					});
					proxy.on('proxyReq', (proxyReq, req, res) => {
						console.log('Sending Request to the Target:', req.method, req.url);
					});
					proxy.on('proxyRes', (proxyRes, req, res) => {
						console.log('Received Response from the Target:', proxyRes.statusCode, req.url);
					});
				},
			},
			// 音乐搜索和音频API代理
			'/api/kuwo': {
				target: 'http://kuwo.bfmzdx.cn',
				changeOrigin: true,
				rewrite: (path) => path.replace(/^\/api\/kuwo/, ''),
				configure: (proxy, options) => {
					proxy.on('error', (err, req, res) => {
						console.log('kuwo proxy error', err);
					});
				},
			},
			// 歌词API代理
			'/api/lyric': {
				target: 'http://openmusic.bfmzdx.cn',
				changeOrigin: true,
				rewrite: (path) => path.replace(/^\/api\/lyric/, ''),
				configure: (proxy, options) => {
					proxy.on('error', (err, req, res) => {
						console.log('lyric proxy error', err);
					});
				},
			},
			// 歌词转换API代理
			'/api/convert': {
				target: 'http://krctottml.music.bfmzdx.cn',
				changeOrigin: true,
				rewrite: (path) => path.replace(/^\/api\/convert/, ''),
				configure: (proxy, options) => {
					proxy.on('error', (err, req, res) => {
						console.log('convert proxy error', err);
					});
				},
			}
		}
	},
	define: {
		global: 'globalThis',
	},
	optimizeDeps: {
		include: [
			"@pixi/app",
			"@pixi/core", 
			"@pixi/display",
			"@pixi/filter-blur",
			"@pixi/filter-bulge-pinch",
			"@pixi/filter-color-matrix",
			"@pixi/sprite"
		]
	},
	build: {
		sourcemap: true,
		outDir: 'dist',
		assetsDir: 'assets',
		target: 'esnext',
		rollupOptions: {
			// 保持为 SPA 构建，不设置 external
		},
	},
	plugins: [
		vue(),
		wasm(),
		topLevelAwait(),
		vueJsx(),
		dts({
			exclude: ["src/test.ts", "src/TestApp.vue"],
		}),
	],
});
